package org.genntii.warehousesystem.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.genntii.warehousesystem.domain.entity.Goods;
import org.genntii.warehousesystem.domain.entity.GoodsOrder;
import org.genntii.warehousesystem.domain.entity.UserAddress;
import org.genntii.warehousesystem.domain.vo.OrderVO;
import org.genntii.warehousesystem.mapper.*;
import org.genntii.warehousesystem.service.GoodsOrderService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
public class GoodsOrderServiceImpl extends ServiceImpl<GoodsOrderMapper, GoodsOrder> implements GoodsOrderService {

    @Resource
    private GoodsOrderMapper goodsOrderMapper;

    @Resource
    private GoodsMapper goodsMapper;

    @Resource
    private CityMapper cityMapper;

    @Resource
    private ProvinceMapper provinceMapper;

    @Resource
    private UserAddressMapper userAddressMapper;

    @Override
    public void createOrder(int goodsId, int num, int warehouseId, int userId, BigDecimal pricePer,int addressId) {
        int codeLength = 6;
        Random random = new Random();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String timeStamp = sdf.format(new Date());
        String randomPart = String.format("%0" + codeLength + "d", random.nextInt((int) Math.pow(10, codeLength)));
        GoodsOrder goodsOrder = GoodsOrder.builder().orderCode(timeStamp+randomPart).goodsId(goodsId).num(num)
                .warehouseId(warehouseId).userId(userId).createTime(LocalDateTime.now()).status(1)
                .pricePer(pricePer).price(pricePer.multiply(BigDecimal.valueOf(num))).addressId(addressId).build();
        goodsOrderMapper.insert(goodsOrder);
    }

    @Override
    public void statusUpdate(int id, int status) {
        goodsOrderMapper.updateStatus(id,status);
    }

    @Override
    public List<OrderVO> selectOrderByPage(int userId, int page) {
        List<GoodsOrder> goodsOrders = goodsOrderMapper.selectOrderByPage(userId,10*(1-page));
        List<OrderVO> voList = new ArrayList<>();
        for (GoodsOrder goodsOrder:goodsOrders){
            OrderVO vo = new OrderVO();
            Goods goods = goodsMapper.selectById(goodsOrder.getGoodsId());
            UserAddress userAddress = userAddressMapper.selectById(goodsOrder.getAddressId());
            vo.setGoodsId(goodsOrder.getGoodsId());
            vo.setName(goods.getName());
            vo.setImgSrc(goods.getImgSrc());
            vo.setNum(goodsOrder.getNum());
            vo.setPrice(goodsOrder.getPrice());
            vo.setPricePer(goodsOrder.getPricePer());
            vo.setOrderCode(goodsOrder.getOrderCode());
            vo.setDate(goodsOrder.getCreateTime());
            vo.setStatus(goodsOrder.getStatus());
            vo.setProvince(provinceMapper.findProvince(cityMapper.findProvinceId(userAddress.getId())));
            vo.setCity(cityMapper.findCity(userAddress.getCityId()));
            vo.setAddress(userAddress.getAddress());
            voList.add(vo);
        }
        return voList;
    }

    @Override
    public List<OrderVO> selectOrderByPageForWarehouse(int warehouseId, int page) {
        List<GoodsOrder> goodsOrders = goodsOrderMapper.selectOrderByPageForWarehouse(warehouseId,10*(page-1));
        List<OrderVO> voList = new ArrayList<>();
        for (GoodsOrder goodsOrder:goodsOrders){
            OrderVO vo = new OrderVO();
            Goods goods = goodsMapper.selectById(goodsOrder.getGoodsId());
            UserAddress userAddress = userAddressMapper.selectById(goodsOrder.getAddressId());
            vo.setGoodsId(goodsOrder.getGoodsId());
            vo.setName(goods.getName());
            vo.setImgSrc(goods.getImgSrc());
            vo.setNum(goodsOrder.getNum());
            vo.setPrice(goodsOrder.getPrice());
            vo.setPricePer(goodsOrder.getPricePer());
            vo.setOrderCode(goodsOrder.getOrderCode());
            vo.setDate(goodsOrder.getCreateTime());
            vo.setStatus(goodsOrder.getStatus());
            vo.setProvince(provinceMapper.findProvince(cityMapper.findProvinceId(userAddress.getId())));
            vo.setCity(cityMapper.findCity(userAddress.getCityId()));
            vo.setAddress(userAddress.getAddress());
            voList.add(vo);
        }
        return voList;
    }
}
